//define(function(require,exports) {
'use strict';

'use strict';
var lifeApp =angular.module('lifeApp', [
    'ngRoute',
    'ngTouch',
    "ngSanitize"
]);
lifeApp
    .config(["$routeProvider","$httpProvider",function ($routeProvider,$httpProvider) {
        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers
            .common['X-Requested-With'];
        $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
        $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
        // Override $http service's default transformRequest
        $httpProvider.defaults.transformRequest = [function(data)
        {
            /**
             * The workhorse; converts an object to x-www-form-urlencoded serialization.
             * @param {Object} obj
             * @return {String}
             */
            var param = function(obj)
            {
                var query = '';
                var name, value, fullSubName, subName, subValue, innerObj, i;

                for(name in obj)
                {
                    value = obj[name];

                    if(value instanceof Array)
                    {
                        for(i=0; i<value.length; ++i)
                        {
                            subValue = value[i];
                            fullSubName = name + '[' + i + ']';
                            innerObj = {};
                            innerObj[fullSubName] = subValue;
                            query += param(innerObj) + '&';
                        }
                    }
                    else if(value instanceof Object)
                    {


                        for(subName in value)
                        {


                            subValue = value[subName];
                            if(subValue != null){
                                // fullSubName = name + '[' + subName + ']';
                                fullSubName = name + '.' + subName;
                                // fullSubName =  subName;
                                innerObj = {};
                                innerObj[fullSubName] = subValue;
                                query += param(innerObj) + '&';
                            }
                        }
                    }
                    else if(value !== undefined ) //&& value !== null
                    {
                        query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
                    }
                }


                return query.length ? query.substr(0, query.length - 1) : query;
            };


            return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
        }];



        $routeProvider
            .when('/', {
                templateUrl: 'life/main.html',
                controller: 'MainCtrl'
            })
            //通讯费
            .when('/tx', {
                templateUrl: 'life/tx.html',
                controller: 'TxCtrl'
            })
            //电费
            .when('/df', {
                templateUrl: 'life/df.html',
                controller: 'DfCtrl'
            })
            //有线电视费
            .when('/yx', {
                templateUrl: 'life/yx.html',
                controller: 'YxCtrl'
            })
            //水费
            .when('/sf', {
                templateUrl: 'life/sf.html',
                controller: 'SfCtrl'
            })
            //气费
            .when('/qf', {
                templateUrl: 'life/qf.html',
                controller: 'QfCtrl'
            })
            //网费
            .when('/wf', {
                templateUrl: 'life/wf.html',
                controller: 'WfCtrl'
            })
            //缴费记录
            .when('/history',{
                templateUrl:'life/history.html',
                controller:'HistoryCtrl'
            })
            //缴费记录详情
            .when('/view/:id',{
                templateUrl:'life/viewhistory.html',
                controller:'ViewhistoryCtrl'
            })
            .when('/success',{
                templateUrl:'life/success.html',
                controller:'SuccessCtrl'
            })
            .when('/error',{
                templateUrl:'life/error.html',
                controller:'ErrorCtrl'
            })
            .when('/read',{
                templateUrl:'life/read.html',
                controller:'ReadCtrl'
            })
            .otherwise({
                redirectTo: '/'
            });
    }]);


//默认加载
lifeApp.run(["$window","$http",function($window,$http){
    //所有交费
    $http.post(hpfbase.url+"/bm/queryBmWapMain.jhtm")
        .success(function(ret){
            if(ret.succeed){
                if(ret.succeed){
                    $window.sessionStorage.lifeList = JSON.stringify(ret.data);
                }
            }
        });
}]);
//扩展
lifeApp.directive("checkLogin",["$window","$http","$location",function($window,$http,$location){
    //登录
    return{
        restrict: 'E',
        replace:true,
        link: function(scope, element) {
            var returnUrl= "life.html";
            $http.get(hpfbase.url+"/user/isLogin.jhtm")
                .success(function(ret){
                    if(!ret.succeed){
                        $window.localStorage.returnUrl = returnUrl;
                        $window.location.href="index.html#/login";
                    }
                }).error(function(){
                    $window.localStorage.returnUrl = returnUrl;
                    $window.location.href="index.html#/login";
                });
        }
    }
}])
    .directive("scrolly",["$window",function($window){
        //分页
        return{
            restrict: 'A',
            link: function(scope, element, attrs) {
                var self = element;
                self.bind("scroll", function (e) {
                    if(self[0].scrollHeight != self[0].clientHeight && (self[0].clientHeight + self[0].scrollTop) == self[0].scrollHeight){
                        scope.$apply(attrs.scrolly);
                    }
                })
            }
        }
    }])
    .directive("readMe",function(){
        //服务条款
        return{
            restrict: 'A',
            templateUrl:"include/readme.html",
            link: function(scope, element, attrs) {
                var oRead =element.find("article");
                var oLink =$('#js_link');
                var oError = $("#js_alert");
                oLink.on("click",function(){
                    oRead.toggleClass("z-sel");
                });
                oRead.on("click",function(){
                    oRead.removeClass("z-sel");
                });
                scope.agree = true;
                $("#js_agree").addClass("z-sel");
                $("#js_agree").on("click",function(){
                    if(!scope.agree){
                        $(this).addClass("z-sel");
                        scope.agree = true;
                        oError.addClass("f-dn");
                        oError.addClass("f-dn");
                    }else{
                        $(this).removeClass("z-sel");
                        scope.agree = false;
                        oError.removeClass("f-dn");
                    }
                });
            }
        }
    })
    .directive("smartTitle",function(){
        //智能标题
        return {
            restrict: 'A',
            scope:true,
            link: function(scope, element, attrs){
                var _text =  scope.pagetitle;
                if(_text){
                    element.html(_text);
                }
            }
        };
    });
//});


var ui = {
    life:"life"

}